// AuxilaryTable.cpp
// Author: Sudhir Meher

#include "AuxilaryTable.h"

#include <iostream>

AuxAffects* AuxAffects::obj = NULL;

AuxAffects::AuxAffects () {
	this->isAffects = NULL;
	this->isAffectsAvailable = NULL;
	this->_numStmts = 0;
	this->bitPosition = 0;
	this->bitLength = sizeof(int) * 8;
	this->cacheSize = 0;
};

AuxAffects* AuxAffects::getInstance() {
	if( AuxAffects::obj== NULL ) {
		obj = new AuxAffects();
		return obj;
	} else
		return obj;
};

void AuxAffects::initialize( int n ) {
	this->_numStmts = n;
	this->cacheSize = ceil((float)_numStmts*_numStmts/sizeof(int)/8);
	this->isAffects = 
		new unsigned int[cacheSize];
	memset ( this->isAffects, 0, cacheSize);

	this->isAffectsAvailable = new unsigned int[cacheSize];
	unsigned int ll = 0; ll =ll - 1;
	// zero all
	for ( int i=0; i<cacheSize; i++ ) {
		isAffectsAvailable[i] = isAffectsAvailable[i]  & 0;
		isAffects[i] = isAffects[i]  & 0;
	}
};

BOOLEAN AuxAffects::affects(STATEMENT_INDEX stmt1, STATEMENT_INDEX stmt2 ){
	this->index = (_numStmts*(stmt1-1)) + stmt2 -1;
	this->bitPosition = index % bitLength;
	//cout << "index: "<<index << "  bit pos: "<< bitPosition <<endl;
	//cout << "affects: " << isAffects[(unsigned int)(floor((double)(index/bitLength)))] << endl;
	return getBit(isAffects[(unsigned int)(index/bitLength)] , (index%bitLength)  );
	/*if ( getBit(isAffects[(unsigned int)(index/bitLength)] , (index%bitLength)  ) == true )
		return true;
	else return false;*/

};

BOOLEAN AuxAffects::isAuxAffectsAvailable (STATEMENT_INDEX stmt1, STATEMENT_INDEX stmt2 ){
	this->index = ((_numStmts*(stmt1-1))) + stmt2-1;
	this->bitPosition = index % bitLength;
	return getBit(isAffectsAvailable[index/sizeof(int)/8] , index%bitLength );
};

STATUS AuxAffects::setAffects ( STATEMENT_INDEX s1, STATEMENT_INDEX s2 ) {
	index = _numStmts*(s1-1) + s2 -1; 
	this->bitPosition = index % bitLength;
	if( index < _numStmts*_numStmts ) {
		setBit(isAffectsAvailable[index/sizeof(int)/8] , index%bitLength); 
		setBit(isAffects[index/sizeof(int)/8] , index%bitLength);
		return STATUS_Success;
	}
	else return STATUS_Failure;
}